package jasper;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;


public class RunTransformation {

	public static void runTransformation(List dataSource) throws Exception {
		String templatePath = "e:\\Develop\\report2.jrxml";
		JasperDesign jasperDesign = JRXmlLoader.load(templatePath);
		JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("logoPath", templatePath+File.separator+"logo.jpg");
		JRDataSource ds = new JRBeanCollectionDataSource(dataSource);
		JasperPrint jasperPrint = JasperFillManager.fillReport(
				jasperReport, parameters, ds);
		
		List printList = new ArrayList();
		printList.add(jasperPrint);
		
//		Apache POI exporter
		JRXlsExporter exporter = new JRXlsExporter();
//		JRXlsxExporter exporter = new JRXlsxExporter();
//		JExcelApiExporter exporter = new JExcelApiExporter();
		
		exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT_LIST, printList);
		exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
//		exporter.setParameter(JRXlsExporterParameter., Boolean.TRUE);
		
		exporter.setParameter(JRXlsExporterParameter.PASSWORD, "password");
		// exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
		// Boolean.TRUE);

		exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
				Boolean.FALSE);
		exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, "e:\\Develop\\report2.xls");
		// net.sf.jasperreports.export.xls.wrap.text
//		<property name="net.sf.jasperreports.export.xls.password" value="sss"/>
		exporter.exportReport();
		}
	
	private void data(){
		LogUnity log = new LogUnity();
		List customerList = new ArrayList();
		for (int i = 0; i < 10; i++) {
			Unit a = new Unit();
			a.setCustomerName("China Unicom "+i);
			a.setVisit(i*10);
			a.setPageView(i*100);
			
			customerList.add(a);
		}
		List userList = new ArrayList();
		for (int i = 0; i < 10; i++) {
			Unit a = new Unit();
			a.setUserName("user "+i);
			a.setVisit(i*10);
			a.setPageView(i*100);
			
			userList.add(a);
		}
		List urlList = new ArrayList();
		for (int i = 0; i < 10; i++) {
			Unit a = new Unit();
			a.setUrl("url "+i);
			a.setVisit(i*10);
			a.setPageView(i*100);
			
			urlList.add(a);
		}
		
		List dailyList = new ArrayList();
		for (int i = 0; i < 10; i++) {
			Unit a = new Unit();
			a.setUrl("url "+i);
			a.setVisit(i*10);
			a.setPageView(i*100);
			
			dailyList.add(a);
		}
		
		log.setCustList(customerList);
		log.setUrlList(urlList);
		log.setUserList(userList);
		log.setDailyList(dailyList);
		
		List source = new ArrayList();
		source.add(log);
		try {
			runTransformation(source);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{custList})
	}
	public static void main(String[] args){
		RunTransformation test = new RunTransformation();
		test.data();
		
	}
	
}
